package com.eims.sls_app.fragment;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import android.app.Activity;
import android.content.res.Resources;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;
import bls.juejin.cn.R;
import progressBar.ProgressDialogBar;

public class CircleFragment_viewpage_god extends Fragment {
	private String TAG = "CircleFragment";
	private Activity activity;

	private ViewPager mPager;
	private ArrayList<Fragment> fragmentsList;
	private ImageView ivBottomLine;
	private TextView tvTabActivity, tvTabGroups, tvTabGroups3;

	private int currIndex = 0;
	private int bottomLineWidth;
	private int offset = 0;
	private int position_one;
	private int position_two;
	private int position_three;
	private Resources resources;

	private MyFragmentPagerAdapter adaper;
	public String ganzhu;
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.fragment_circle_zq_viewpager_god, container, false);
		activity = getActivity();
		// transaction = getActivity().getSupportFragmentManager()
		// .beginTransaction();
		resources = getResources();

		Log.e("11", "1");
		InitWidth(view);
		InitTextView(view);
		InitViewPager(view);

		return view;
	}

	private void InitTextView(View view) {
		Log.e("11", "3");
		tvTabActivity = (TextView) view.findViewById(R.id.tv_tab_activity);
		tvTabGroups = (TextView) view.findViewById(R.id.tv_tab_groups);
		tvTabGroups3 = (TextView) view.findViewById(R.id.tv_tab_groups3);

		tvTabActivity.setOnClickListener(new MyOnClickListener(0));
		tvTabGroups.setOnClickListener(new MyOnClickListener(1));
		tvTabGroups3.setOnClickListener(new MyOnClickListener(2));
	}

	private void InitViewPager(View view) {
		ProgressDialogBar.startProgress(activity, "sff", false);
		Log.e("11", "4");
		mPager = (ViewPager) view.findViewById(R.id.vPager);
		fragmentsList = new ArrayList<Fragment>();
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("userid", "小洪");
		paramMap.put("age", 23);
		if (fragmentsList != null) {
			fragmentsList.clear();
			Log.e("11", "14");
		}
		// Fragment activityfragment = TestFragment.newInstance("Hello",paramMap);
		// Fragment groupFragment = TestFragment.newInstance("Hello Group.",paramMap2);

		//
		// fragmentsList.add(activityfragment);
		// fragmentsList.add(groupFragment);

		Fragment activityfragment = new CircleFragment_cirFrament_fensheng_hongdan();
		Fragment groupFragment = new CircleFragment_cirFrament_fensheng_bodan();
		Fragment groupFragment3 = new CircleFragment_cirFrament_fensheng_jiangjin();

		fragmentsList.add(activityfragment);
		fragmentsList.add(groupFragment);
		fragmentsList.add(groupFragment3);

		adaper = new MyFragmentPagerAdapter(getChildFragmentManager(), fragmentsList);
		adaper.notifyDataSetChanged();
		mPager.setAdapter(adaper);

		mPager.setCurrentItem(0);
		mPager.setOnPageChangeListener(new MyOnPageChangeListener());
		// 设置缓存的fragment数量
		mPager.setOffscreenPageLimit(3);
	}

	@Override
	public void onDestroy() {
		// TODO Auto-generated method stub
		fragmentsList.clear();
		Log.e("1", "5");
		super.onDestroy();
	}

	@Override
	public void onStop() {
		// TODO Auto-generated method stub
		Log.e("1", "6");
		super.onStop();
	}

	@Override
	public void onPause() {
		Log.e("1", "7");
		// TODO Auto-generated method stub
		super.onPause();
	}

	private void InitWidth(View view) {
		Log.e("11", "2");
		ivBottomLine = (ImageView) view.findViewById(R.id.iv_bottom_line);
		int h = getActivity().getResources().getDisplayMetrics().heightPixels;
		int w = getActivity().getResources().getDisplayMetrics().widthPixels;
		LayoutParams lp;
		lp = (LayoutParams) ivBottomLine.getLayoutParams();
		lp.width = w / 3;
		ivBottomLine.setLayoutParams(lp);

		bottomLineWidth = ivBottomLine.getLayoutParams().width;
		Log.d(TAG, "cursor imageview width=" + bottomLineWidth);
		DisplayMetrics dm = new DisplayMetrics();
		getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);
		int screenW = dm.widthPixels;
		offset = (int) ((screenW / 3.0 - bottomLineWidth) / 2);

		position_one = (int) (screenW / 3.0);
		position_two = position_one * 2;
		position_three = position_one * 2;
	}

	public class MyOnClickListener implements View.OnClickListener {
		private int index = 0;

		public MyOnClickListener(int i) {
			index = i;
		}

		@Override
		public void onClick(View v) {
			mPager.setCurrentItem(index);
		}
	};

	public class MyOnPageChangeListener implements OnPageChangeListener {

		@Override
		public void onPageSelected(int arg0) {
			Animation animation = null;
			switch (arg0) {
			case 0:
				if (currIndex == 1) {
					animation = new TranslateAnimation(position_one, 0, 0, 0);
					tvTabGroups.setTextColor(resources.getColor(R.color.black));
				} else if (currIndex == 2) {
					animation = new TranslateAnimation(position_two, 0, 0, 0);
					tvTabGroups3.setTextColor(resources.getColor(R.color.black));
				}
				tvTabActivity.setTextColor(resources.getColor(R.color.black));
				break;
			case 1:
				if (currIndex == 0) {
					animation = new TranslateAnimation(0, position_one, 0, 0);
					tvTabActivity.setTextColor(resources.getColor(R.color.black));
				} else if (currIndex == 2) {
					animation = new TranslateAnimation(position_two, position_one, 0, 0);
					tvTabGroups3.setTextColor(resources.getColor(R.color.black));
				}
				tvTabGroups.setTextColor(resources.getColor(R.color.black));
				break;
			case 2:
				if (currIndex == 0) {
					animation = new TranslateAnimation(0, position_two, 0, 0);
					tvTabActivity.setTextColor(resources.getColor(R.color.black));
				} else if (currIndex == 1) {
					animation = new TranslateAnimation(position_one, position_two, 0, 0);
					tvTabGroups.setTextColor(resources.getColor(R.color.black));
				}
				tvTabGroups3.setTextColor(resources.getColor(R.color.black));
				break;
			}
			currIndex = arg0;
			animation.setFillAfter(true);
			animation.setDuration(300);
			ivBottomLine.startAnimation(animation);
		}

		@Override
		public void onPageScrolled(int arg0, float arg1, int arg2) {
		}

		@Override
		public void onPageScrollStateChanged(int arg0) {
		}
	}

}
